package com.guang;

import com.guang.bean.Account;
import com.guang.bean.Blog;
import com.guang.bean.Comment;
import com.guang.mapper.AccountMapper;
import com.guang.mapper.BlogMapper;
import com.guang.mapper.CommentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * @author liguang
 * @date 2022/5/23 17:09
 */
public class MyBatisNestedTest1 {

    private SqlSessionFactoryBuilder builder = null;
    private InputStream is = null;
    private SqlSessionFactory factory = null;
    private Connection connection = null;
    @Before
    public void init() throws IOException, SQLException {
        //1. 读取全局配置文件SqlMapConfig.xml
        is = Resources.getResourceAsStream("sqlmapconfig.xml");
        //2. 创建SqlSessionFactoryBuilder构造者对象
        builder = new SqlSessionFactoryBuilder();
        factory = builder.build(is);
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "root", "root");
    }

    @After
    public void destroy() throws IOException {
        is.close();
    }

    /**
     * 用户去查询银行卡，然后查看银行卡的所属人
     */
    @Test
    public void testOne(){
        SqlSession sqlSession = factory.openSession();
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        Comment comment = commentMapper.findCommentById(1);
        System.out.println(comment);
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Blog blogById = mapper.findBlogById(1);
        System.out.println(blogById);
    }

    @Test
    public void testTwo(){
        SqlSession sqlSession = factory.openSession();
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        // 主查询来进行执行，然后进行属性填充阶段，先来填充blog，填充blog的时候发现有commet继续来进行填充，会从缓存中来进行查找，然后查看是否有对应的值
        Comment comment = commentMapper.findCommentById(1);
        // System.out.println(comment);
        System.out.println("hello,world");
    }


}
